<template>
  <div class="app-container">
    <div class="form-box">
      <el-form ref="form" :model="form" label-width="120px" :rules="rules">
        <el-tabs type="border-card" class="form-tabs">
          <el-tab-pane :label="$t('common.edit_info')" class="form-tabs-tab-pane">
            <!-- 标题 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.title')" prop="title">
                  <el-input v-model="form.title" :placeholder="$t('common.title_tips')" clearable />
                </el-form-item>
              </el-col>
            </el-row>

            <!-- 封面图 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.cover_image')"
                  prop="cover_image"
                >
                  <jsp-image-upload
                    :title="$t('common.cover_image')"
                    :default-list="cover_imageList"
                    :action="fileUploadConfig.imageAction"
                    :headers="fileUploadConfig.headers"
                    :size="Number(fileUploadConfig.allowImageSize)"
                    :accept="fileUploadConfig.allowImageTypeList"
                    @error="(error)=>{error!=null && $message.error(error)}"
                    @change="handleImageUploadChange"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 关键词 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.keywords')" prop="keywords">
                  <jsp-tags
                    :data="form.keywords"
                    :limit="20"
                    @change=" (tags) => form.keywords = tags "
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 摘要 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item
                  :label="$t('common.description')"
                  prop="description"
                >
                  <el-input
                    v-model="form.description"
                    type="textarea"
                    :placeholder="$t('common.description_tips')"
                  />
                </el-form-item>
              </el-col>
            </el-row>
            <!-- 内容 -->
            <el-row :gutter="20" type="flex" justify="center">
              <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
                <el-form-item :label="$t('common.content')" prop="content">
                  <jsp-ckeditor
                    id="content"
                    v-model="form.content"
                    :config="{
                      'filebrowserUploadUrl': fileUploadConfig.action,
                      'filebrowserImageUploadUrl': fileUploadConfig.imageAction,
                      'headers' : fileUploadConfig.headers,
                      'filebrowserUploadInputName': 'file',
                      'uploadFileDomain' : fileUploadConfig.uploadFileDomain,
                    }"
                  />
                </el-form-item>
              </el-col>
            </el-row>
          </el-tab-pane>
        </el-tabs>

        <el-row :gutter="20" type="flex" justify="center" class="btn-row">
          <el-col :xs="24" :sm="24" :md="20" :lg="18" :xl="16">
            <el-form-item>
              <el-button type="primary" :disabled="!checkPermission(['admin.infoPage.update'])" :loading="submitLoading" @click="onSubmit">{{ $t('common.submit') }}</el-button>
              <el-button @click="onCancel">{{ $t('common.cancel') }}</el-button>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
    </div>
  </div>
</template>

<script>
import JspCkeditor from '@/components/JspCkeditor'
import JspImageUpload from '@/components/JspImageUpload'
import JspTags from '@/components/JspTags'
import { show, update } from '@/api/info-page'
import { mapGetters } from 'vuex'
import { loadingStart } from '@/utils/loading-service'
import checkPermission from '@/utils/permission'

// export
export default {
  name: 'InfoPageEdit',
  components: {
    JspCkeditor,
    JspImageUpload,
    JspTags
  },
  data() {
    return {
      submitLoading: false,
      form: {
        title: '',
        cover_image: 0,
        keywords: [],
        description: '',
        content: '',
        created_at: ''
      },
      // 封面图列表: 请求附件接口
      cover_imageList: [
      ],
      // 表单输入校验 ，通过prop="name"属性进行控制
      rules: {
        type: [{ required: true, message: this.$t('navigation.type_tips'), trigger: ['change'] }],
        name: [
          { required: true, message: this.$t('navigation.name_tips'), trigger: 'blur' },
          { min: 2, max: 50, message: this.$t('navigation.name_length_tips'), trigger: 'blur' }
        ]
      },
      // 路由参数
      routerParams: {},
      infoCategory: {}
    }
  },
  computed: {
    ...mapGetters(['fileUploadConfig'])
  },
  created() {
    this.routerParams = this.$route.params
    this.infoCategory = this.$route.meta.infoCategory
  },
  mounted() {
    this.getDetail()
  },
  methods: {
    async getDetail() {
      const loading = loadingStart()
      await show(this.infoCategory.id).then((response) => {
        this.form = response.data
      }).catch(() => {
        this.closeBack()
      })
      // 封面图
      this.cover_imageList = this.form.cover_image
      this.form.cover_image = this.form.cover_image.map((item) => {
        return item.id
      }).join(',')
      loading.close()
    },
    onSubmit() {
      this.$refs['form'].validate(async(valid) => {
        if (valid) {
          this.submitLoading = true
          await update(this.infoCategory.id, this.form).then((response) => {
            this.$message.success(response.message)
          }).catch(() => {})
          this.submitLoading = false
        } else {
          this.$message.error(this.$t('common.pleaseFillRequired'))
          return false
        }
      })
    },
    onCancel() {
      this.closeBack()
    },
    handleImageUploadChange(result) {
      this.form.cover_image = result.ids
    },
    checkPermission
  }
}
</script>

<style scoped lang="scss">
</style>
